package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.EnvInfor;
import com.suyuan.entity.FeedingInfor;
import com.suyuan.entity.Feeds;
import com.suyuan.entity.PigOut;
import com.suyuan.service.FeedingService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 饲养信息控制类
 */
@Controller
@RequestMapping("/admin/Feeding")
public class FeedingController {

    @Resource
    private FeedingService feedingService;
    private Map<String,Object> msg;

    /**
     * 条件查询饲养信息
     * @param beginTime
     * @param endTime
     * @param purGid
     * @param page
     * @param modelMap
     * @return
     */
    @RequestMapping("findByCondition")
    public String inquiry(Date beginTime, Date endTime, Integer purGid, Page page, ModelMap modelMap){
        PageInfo pageInfo  = feedingService.inquiry(beginTime,endTime,purGid,page);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("endTime",endTime);
        modelMap.addAttribute("endTime",endTime);
        modelMap.addAttribute("page",page);
        return "feeding_list";
    }


    /**
     * 添加饲养信息
     * @param feedingInfor
     * @return
     */
    @RequestMapping("addFeeding")
    @ResponseBody
    public Object addFeedingInfo(FeedingInfor feedingInfor){
        int code = 0;
        if(feedingInfor == null){
            return 0;
        }
        try{
           feedingService.addFeedingInfo(feedingInfor);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }


    /**
     * 跳转到添加饲养信息页面
     * @param modelMap
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(ModelMap modelMap){
        List<Feeds> feedsList = feedingService.findAllFeed();
        modelMap.addAttribute("feedsList",feedsList);
        return "feeding_add";
    }


    /**
     * 删除饲养信息
     * @param
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Object deleteInfo(String ids){
        Integer code = feedingService.deleteFeedingInfo(ids);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Integer deleteInfo(Integer id){
        return feedingService.deleteByGid(id);
    }


    /**
     * 跳转编辑页面
     * @param gid
     * @return
     */
    @RequestMapping("toedit")
    public String toedit(Integer gid ,ModelMap modelMap){
        FeedingInfor feedingInfor =feedingService.findByGid(gid);
        modelMap.addAttribute("feedingInfo",feedingInfor);
        return "feeding_edit";
    }


    /**
     * 更新饲养信息
     * @param feedingInfor
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Integer updateInfo(FeedingInfor feedingInfor){
        int code = 0;
        try{
            feedingService.updateInfo(feedingInfor);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }



}
